﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Pibby.Core.Tests.Stubs
{
	public class WindowsPlaylistGenerator : IGenerateContent
	{
		private StringBuilder _builder = new StringBuilder();

		#region Constructors

		public WindowsPlaylistGenerator()
		{
			this.Version = "1.0";
			this.Generator = "Pibby Media Player -- 1.0.0.0";
		}

		#endregion Constructors

		#region Properties

		public string Version
		{
			get;
			set;
		}

		public string Generator
		{
			get;
			set;
		}

		public int HowManyItems
		{
			get;
			set;
		}

		public int Duration
		{
			get;
			set;
		}

		#endregion Properties

		#region Generation

		private void CreatePlaylist()
		{
			BuildHeader();

			for (int i = 0; i < this.HowManyItems; ++i)
			{
				BuildItem(string.Format("..\\My Music\\Music_{0}.mp3", i + 1), Guid.NewGuid ().ToString ());
			}

			BuildFooter();
		}

		public void BuildHeader()
		{
			this._builder.AppendFormat("<?wpl version=\"{0}\"?>", this.Version);

			this._builder.AppendLine("<smil>");
			this._builder.AppendLine("<head>");

			this._builder.AppendFormat("<meta name=\"Generator\" content=\"{0}\"/>", this.Generator);
			this._builder.AppendFormat("<meta name=\"AverageRating\" content=\"50\"/>");
			this._builder.AppendFormat("<meta name=\"TotalDuration\" content=\"{0}\"/>", this.Duration);
			this._builder.AppendFormat("<meta name=\"ItemCount\" content=\"{0}\"/>", this.HowManyItems);

			this._builder.AppendLine("<title>Example Playlist</title>");
			this._builder.AppendLine("</head>");
			this._builder.AppendLine("<body>");
			this._builder.AppendLine("<seq>");
		}

		public void BuildItem(string relativePath, string guid)
		{
			this._builder.AppendFormat("<media src=\"{0}\" tid=\"{1}\"/>", relativePath, guid);
		}

		public void BuildFooter()
		{
			this._builder.AppendLine("</seq>");
			this._builder.AppendLine("</body>");
			this._builder.AppendLine("</smil>");
		}

		#endregion Generation


		#region IGenerateContent

		public string Content
		{
			get
			{
				this.CreatePlaylist();

				return this._builder.ToString();
			}
		}

		public TextReader ContentStream
		{
			get
			{
				return new StringReader(this.Content);
			}
		}


		#endregion
	}
}
